package cn.xiaobai.j2exe.domain;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.annotation.JSONField;
import com.alibaba.fastjson2.annotation.JSONType;

import java.io.File;
import java.io.Serializable;

/**
 * 缓存配置
 * {
 * "initialDirCache":"目录缓存",
 * "confLocation":"配置存放"
 * }
 *
 * @Author yangdaji
 * @Date 2023/12/27
 * @PackageName cn.xiaobai.j2exe.domain
 * @ClassName CacheConfig
 */
@JSONType
public class CacheConfig implements Serializable {

    /**
     * 初始目录缓存
     */
    @JSONField(serializeUsing = File.class, deserializeUsing = File.class)
    private File initialDirCache;

    /**
     * 配置文件所在位置
     */
    @JSONField(serializeUsing = File.class, deserializeUsing = File.class)
    private File confLocation;


    /**
     * 输出目录缓存
     */
    @Deprecated
    @JSONField(serializeUsing = File.class, deserializeUsing = File.class)
    private File outputDirCache;

    /**
     * 是否更新
     */
    private transient boolean update;


    public void copyData(CacheConfig source) {
        BeanUtil.copyProperties(source, this);
    }

    public CacheConfig() {
    }

    public File getInitialDirCache() {
        return initialDirCache;
    }

    public void setInitialDirCache(File initialDirCache) {
        if (initialDirCache == null) {
            return;
        }
        if (!ObjectUtil.equal(this.initialDirCache, initialDirCache)) {
            this.initialDirCache = initialDirCache;
            this.update = true;
        } else {
            setUpdate(false);
        }
    }

    public File getConfLocation() {
        return confLocation;
    }

    public void setConfLocation(File confLocation) {
        if (confLocation == null) {
            return;
        }
        // 弱安全过滤
        if (confLocation.getName().endsWith(".json") &&
                !ObjectUtil.equal(this.confLocation, confLocation)) {
            this.confLocation = confLocation;
            this.update = true;
        } else {
            setUpdate(false);
        }
    }

    public File getOutputDirCache() {
        return outputDirCache;
    }

    public void setOutputDirCache(File outputDirCache) {
        if (outputDirCache == null) {
            return;
        }
        if (!ObjectUtil.equal(this.outputDirCache, outputDirCache)) {
            this.outputDirCache = outputDirCache;
            this.update = true;
        } else {
            setUpdate(false);
        }
    }


    public boolean getUpdate() {
        return update;
    }

    public void setUpdate(boolean update) {
        this.update = update;
    }
}
